//引入
const express = require('express');
const db = require('../../utils/db')

//配置路由器
const renshi = express.Router();

//接口编写
//获取
renshi.get('/renshi/query', (req, res) => {
  const page = req.query.page;
  const count = req.query.count;
  const sql = `select * from yuangong limit ${(page - 1) * count},${count}`;
  const sql2 = `select count(*) as total from yuangong;`;
  db.query(sql, (err, data) => {
    if (err) {
      console.log(err)
      res.send({ error: 1, msg: err })
    } else {
      db.query(sql2, (err2, data2) => {
        if (err2) {
          console.log(err2)
          res.send({ error: 1, msg: err2 })
        } else {
          console.log(data2)
          res.send({ error: 0, msg: '查询成功', data: data, total: data2[0].total })
        }
      })
    }
  })
})
//查询搜索
renshi.post('/renshi/sousuo', (req, res) => {
  const sql = `SELECT * FROM yuangong WHERE  y_bumen LIKE'%${req.body.number}%' OR y_name='${req.body.name}';`
  db.query(sql, (err, data) => {
      if (err) {
          console.log(err);
          res.send({ error: 1, msg: err })
      } else {
          res.send({ error: 0, msg: '查询成功', data: data })
      }
  })
})


// 删除
renshi.post('/renshi/del', (req, res) => {
  const sql = 'delete from yuangong where y_id="' + req.body.id + '";';
  db.query(sql, (err, data) => {
      if (err) {
          console.log(err);
          res.send({ error: 1, msg: err })
      } else {
          res.send({ error: 0, msg: '删除成功' })
      }
  })
})
//修改
renshi.post('/renshi/modify',(req,res)=>{
  const sql=`update yuangong set y_id='${req.body.id}',y_bumen='${req.body.bumen}',y_name='${req.body.name}',y_youxiang='${req.body.youxiang}',y_gonghao='${req.body.gonghao}',y_ruzhi='${req.body.ruzhi}',y_gonghao='${req.body.gonghao}',y_ruzhi='${req.body.ruzhi}';`
  db.query(sql,(err,data)=>{
      if(err){
          console.log(err);
          res.send({error:1,msg:err})
      }else{
          res.send({error:0,msg:'修改成功',data:data})
      }
  })
})
//导出
module.exports = renshi;